package com.xnx3.banmahuishou.agency.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xnx3.DateUtil;
import com.xnx3.DoubleUtil;
import com.xnx3.banmahuishou.core.entity.Daishoudian;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.wangmarket.shop.core.entity.Order;
import com.xnx3.wangmarket.shop.core.entity.OrderGoods;

/**
 * 首页管理
 * @author zxy
 */
@Controller(value="BanmahuishouAgencyShouYeController")
@RequestMapping("/agency/shouye/")
public class ShouYeController extends BaseController {
	@Resource
	private SqlService sqlService;	

	/**
	 * 代理商后台首页
	 */
	@RequestMapping("list${url.suffix}")
	public String list(HttpServletRequest request,Model model){
		int dailishangid = getUserId();
		//当前时间戳
		int timenow = DateUtil.timeForUnix10();
		//本日0点0分时间戳
		int dayZero = DateUtil.getDateZeroTime(timenow);
		//昨日零点时间戳
		int yesterdayZero = dayZero - 86400;
		//明天零点时间戳
		int daynight = dayZero + 86400;

		//取出该代理商的所有订单
		String sqlTotal = "SELECT * FROM shop_order WHERE  dailishang_userid = " + dailishangid;
		List<Order> listTotal = sqlService.findBySqlQuery(sqlTotal, Order.class);
		int changdu = listTotal.size();
		//总订单的数量
		int orderCount = 0;
		//总成交金额 单位分
		int moneySum = 0;
		//总回收的重量
		int allSum = 0;
		if (changdu > 0) {
			for (Order orderTotal : listTotal) {
				//订单是已完成待评价 或者 已经完成已评价的
				if(ObjectUtils.equals(orderTotal.getState(), Order.STATE_FINISH) || ObjectUtils.equals(orderTotal.getState(), Order.STATE_EVALUATED ) ) {
					//总订单的数量
					orderCount = orderCount + 1;
					//总成交金额 单位分
					moneySum = moneySum + orderTotal.getPayMoney();
					//总回收的重量
					allSum = allSum + orderTotal.getAllWeight();
				}

			}
		}
		//总订单的数量
		model.addAttribute("orderCount", orderCount);
		//总成交金额 单位分
		model.addAttribute("moneySum",DoubleUtil.doubleSplit( moneySum / 100,2));
		//总回收的重量
		model.addAttribute("allSum", DoubleUtil.doubleSplit( allSum / 1000 ,2));
		
		//取出该代理商今天的订单(预约开始时间是今日的 即 预约开始时间大于今日零点且小于明日零点)
		String sqlToday = "SELECT * FROM shop_order WHERE  dailishang_userid = " + dailishangid + " AND reserve_start_time > " + dayZero +  " AND reserve_start_time < " + daynight;
		List<Order> listToday = sqlService.findBySqlQuery(sqlToday, Order.class);
		int orderchangdu = listToday.size();
		//今日订单总量
		int ordernowCount = orderchangdu; 
		model.addAttribute("ordernowCount", ordernowCount);
		
		//今日服务中单数
		int waitingCount = 0 ;
		//今日取消单数
		int cancelledCount = 0;
		//今日已超时数量
		int chaoshiCount = 0;
		//今日即将超时数量（临近30分钟）
		int doorCount = 0;
		//今日已完成单数
		int doneCount = 0;
		//今日好评数量
		int haopingCount = 0;
		//今日差评数量
		int chapingCount = 0;
		//今日其他评价数量
		int qitaCount = 0;
		
		//今日订单成交金额 单位分
		int moneynowSum = 0;
		//今日回收重量 单位克
		int allnowSum = 0;
		
		if (orderchangdu > 0) {
			for (Order orderToday : listToday) {
				//服务中: 预约结束时间大于当前时间且状态为2
				if (ObjectUtils.equals(orderToday.getState(), Order.STATE_FINISH) && orderToday.getReserveEndTime() > timenow) {
					//今日服务中单数
					waitingCount = waitingCount + 1;
				}
				//已取消: 订单状态为4
				if (ObjectUtils.equals(orderToday.getState(), Order.STATE_CANCEL)) {
					//今日取消单数
					cancelledCount = cancelledCount + 1;
				}
				//已超时: 预约结束时间小于当前时间且状态为2
				if ( ObjectUtils.equals(orderToday.getState(), Order.STATE_DAIQUHUO) && orderToday.getReserveEndTime() < timenow ) {
					//今日已超时数量
					chaoshiCount = chaoshiCount + 1;
				}
				//即将超时: 预约结束时间小于当前时间前30分钟且状态为2
				if (ObjectUtils.equals(orderToday.getState(), Order.STATE_DAIQUHUO) && (orderToday.getReserveEndTime() - timenow) < 1800 && (orderToday.getReserveEndTime() - timenow) > 0) {
					//今日即将超时数量（临近30分钟）
					doorCount = doorCount + 1;
				}
				//已完成: 订单状态是3或者是5
				if ((ObjectUtils.equals(orderToday.getState(), Order.STATE_FINISH) || ObjectUtils.equals(orderToday.getState(), Order.STATE_EVALUATED))) {
					//今日已完成单数
					doneCount = doneCount + 1;
					//今日订单成交金额 单位分
					moneynowSum = moneynowSum + orderToday.getPayMoney();
					//今日回收重量 单位克
					allnowSum = allnowSum + orderToday.getAllWeight();
				}
				//好评: 大于等于4分, 注意数据库是40
				if (ObjectUtils.equals(orderToday.getState(), Order.STATE_EVALUATED) && orderToday.getEvaluationScore() >= 40) {
					//今日好评数量
					haopingCount = haopingCount + 1;
				}
				//差评: 小于等于2分
				if (ObjectUtils.equals(orderToday.getState(), Order.STATE_EVALUATED) && orderToday.getEvaluationScore() <= 20 && orderToday.getEvaluationScore() >= 0) {
					//今日差评数量
					chapingCount = chapingCount + 1;
				}
				//其他: 大于2分且小于4分
				if (ObjectUtils.equals(orderToday.getState(), Order.STATE_EVALUATED) && orderToday.getEvaluationScore() < 40 && orderToday.getEvaluationScore() > 20) {
					//今日其他评论数量
					qitaCount = qitaCount + 1;
				}

			}
		}
		//今日服务中单数
		model.addAttribute("waitingCount", waitingCount);
		//今日取消单数
		model.addAttribute("cancelledCount", cancelledCount);
		//今日已超时数量
		model.addAttribute("chaoshiCount", chaoshiCount);
		//今日即将超时数量（临近30分钟）
		model.addAttribute("doorCount", doorCount);
		//今日已完成单数
		model.addAttribute("doneCount", doneCount);
		//今日订单成交金额 
		model.addAttribute("moneynowSum",DoubleUtil.doubleSplit( moneynowSum / 100 ,2));
		//今日回收重量
		model.addAttribute("allnowSum",DoubleUtil.doubleSplit( allnowSum / 1000 ,2));
		
		//今日好评数量
		model.addAttribute("haopingCount", haopingCount);
		//今日差评数量
		model.addAttribute("chapingCount", chapingCount);
		//今日其他评论数量
		model.addAttribute("qitaCount", qitaCount);
		
		
		//总用户个数
		int userCount = sqlService.count("user", "WHERE authority = 11 AND dailishang_userid = " + dailishangid) ;
		model.addAttribute("userCount", userCount);
		
		//今日新增用户个数
		int usernowCount = sqlService.count("user", "WHERE authority = 11 AND dailishang_userid = " + dailishangid + " AND regtime >= " + dayZero + " AND regtime < " + timenow) ;
		model.addAttribute("usernowCount", usernowCount);
		
		//昨日新增用户个数
		int useryesterdayCount = sqlService.count("user", "WHERE authority = 11 AND dailishang_userid = " + dailishangid + " AND regtime >= " + yesterdayZero + " AND regtime < " + dayZero) ;
		model.addAttribute("useryesterdayCount", useryesterdayCount);
		
		//今日新增订单数量
		int doneorderCount = sqlService.count("shop_order", "WHERE dailishang_userid = " + dailishangid + " AND addtime > " + dayZero +  " AND addtime < " + daynight );
		model.addAttribute("doneorderCount", doneorderCount);
		
		//昨日新增订单数量
		int doneyesterdayCount = sqlService.count("shop_order", "WHERE dailishang_userid = " + dailishangid + " AND addtime > " + yesterdayZero +  " AND addtime < " + dayZero );; 
		model.addAttribute("doneyesterdayCount", doneyesterdayCount);
		
		//昨日订单
		String  sqlzr = "SELECT * FROM shop_order WHERE  dailishang_userid = " + dailishangid + " AND reserve_start_time > " + yesterdayZero +  " AND reserve_start_time < " + dayZero;
		List<Order> listzr = sqlService.findBySqlQuery(sqlzr, Order.class);
		int zr = listzr.size();
		//昨日订单成交金额 单位分
		int moneyyesterdaySum = 0;
		//昨日回收重量 单位克
		int allyesterdaySum = 0;
		if (zr > 0) {
			for (Order orderzr : listzr) {
				//已完成: 订单状态是3或者是5
				if ((ObjectUtils.equals(orderzr.getState(), Order.STATE_FINISH) || ObjectUtils.equals(orderzr.getState(), Order.STATE_EVALUATED))) {
					//昨日订单成交金额 单位分
					moneyyesterdaySum = moneyyesterdaySum + orderzr.getPayMoney();
					//昨日回收重量 单位克
					allyesterdaySum = allyesterdaySum + orderzr.getAllWeight();
				}
				
			}
		}
		//昨日订单成交金额
		model.addAttribute("moneyyesterdaySum", DoubleUtil.doubleSplit(moneyyesterdaySum / 100 ,2) );
		//昨日回收重量 
		model.addAttribute("allyesterdaySum", DoubleUtil.doubleSplit(allyesterdaySum / 1000 ,2));
		
		//代收点总数量
		int storeCount = sqlService.count("laji_daishoudian", "WHERE state = " + Daishoudian.STATE_APPROVED + " AND dailishang_userid = " + dailishangid) ;
		model.addAttribute("storeCount", storeCount);
		//今日新增回收点数量
		int storenowCount = sqlService.count("laji_daishoudian", "WHERE state = " + Daishoudian.STATE_APPROVED + " AND dailishang_userid = " + dailishangid + " AND create_time > " + dayZero + " AND create_time < " + daynight) ;
		model.addAttribute("storenowCount", storenowCount);
		//昨日新增代收点数量
		int storeyesterdayCount = sqlService.count("laji_daishoudian", "WHERE state = " + Daishoudian.STATE_APPROVED + " AND dailishang_userid = " + dailishangid + " AND create_time > " + yesterdayZero + " AND create_time < " + dayZero) ;
		model.addAttribute("storeyesterdayCount", storeyesterdayCount);
		
		int feizhi = 0;
		int suliao = 0;
		int jinshu = 0;
		int jiuyiwu = 0;
		int jiayongdianqi = 0;
		int shoujishuma = 0;
		int gongyeshebei = 0;
		int jianzhucailiao = 0;
		int xiangjiao = 0;
		int pinglei = 0;

		//今日订单内的商品 
		String sqlgoods = "SELECT * FROM shop_order_goods WHERE orderid in ("
				+ " SELECT id FROM shop_order WHERE dailishang_userid = " + dailishangid + " AND reserve_start_time > " + dayZero +  " AND reserve_start_time < " + daynight + " AND (state = " + Order.STATE_FINISH + " OR state = " + Order.STATE_EVALUATED + ")"+ ")";
		List<OrderGoods> listgoods = sqlService.findBySqlQuery(sqlgoods, OrderGoods.class);
		int goodsCd = listgoods.size();
		if (goodsCd > 0) {
			for (OrderGoods orderGoods : listgoods) {
				if (orderGoods.getGoodsTypeId() - 7 ==0) {
					feizhi = feizhi + orderGoods.getNumber();
				}
				if (orderGoods.getGoodsTypeId() - 8 ==0) {
					suliao = suliao + orderGoods.getNumber();
				}
				if (orderGoods.getGoodsTypeId() - 10 ==0) {
					jinshu = jinshu + orderGoods.getNumber();
				}
				if (orderGoods.getGoodsTypeId() - 11 ==0) {
					jiuyiwu = jiuyiwu + orderGoods.getNumber();
				}
				if (orderGoods.getGoodsTypeId() - 12 ==0) {
					jiayongdianqi = jiayongdianqi + orderGoods.getNumber();
				}
				if (orderGoods.getGoodsTypeId() - 13 ==0) {
					shoujishuma = shoujishuma + orderGoods.getNumber();
				}
				if (orderGoods.getGoodsTypeId() - 14 ==0) {
					gongyeshebei = gongyeshebei + orderGoods.getNumber();
				}
				if (orderGoods.getGoodsTypeId() - 15 ==0) {
					jianzhucailiao = jianzhucailiao + orderGoods.getNumber();
				}
				if (orderGoods.getGoodsTypeId() - 16 ==0) {
					xiangjiao = xiangjiao + orderGoods.getNumber();
				}
				if (orderGoods.getGoodsTypeId() - 17 ==0) {
					pinglei = pinglei + orderGoods.getNumber();
				}
			}
		}

		model.addAttribute("feizhi",feizhi);
		model.addAttribute("suliao",  suliao);
		model.addAttribute("jinshu", jinshu );
		model.addAttribute("jiuyiwu", jiuyiwu);
		model.addAttribute("jiayongdianqi", jiayongdianqi);
		model.addAttribute("shoujishuma", shoujishuma );
		model.addAttribute("gongyeshebei", gongyeshebei);
		model.addAttribute("jianzhucailiao",  jianzhucailiao);
		model.addAttribute("xiangjiao", xiangjiao );
		model.addAttribute("pinglei",pinglei );

		return "/agency/shouye/list";

	}



}
